package com.thoughtworks;


public  class LibraryApp {
    public enum Operations implements PerformOperation {
        LISTALL {
            public void performOperation(Library library) {
                library.listAllEntities();
                library.outputProcess.showGeneralMenu();
               // return userId;
            }
        },
        LOGIN {
            public void performOperation(Library library) throws NotAValidUser {
               String userId = library.inputProcess.getUserId();
                String password = library.inputProcess.getUserPassword();

                boolean isTheUserValid = library.logIn(userId, password);
                if (!isTheUserValid)
                    library.outputProcess.showGeneralMenu();
                else {
                    String userName = library.getUserName(userId);
                    library.outputProcess.printWelcomeMessage(userName);
                    if (userName.equals("librarian")) {
                        library.setCurrentUser(userId);
                        library.outputProcess.showLibrarianMenu();
                    }
                    else{
                        library.setCurrentUser(userId);
                        library.outputProcess.showCustomerMenu();}

                }
                //return userId;
            }

        },

        CHECKOUTBOOK {
            public void performOperation(Library library) {
                boolean isTheOperationSuccessful = false;
                String bookName = library.inputProcess.getEntityName("book", "check out");


                try {
                    isTheOperationSuccessful = library.checkOut(bookName, library.getCurrentUser(), Book.class);
                } catch (Exception e) {
                    System.out.println(e);
                }
                if (isTheOperationSuccessful) {
                    library.outputProcess.printThankYouMessageForCheckingOut(Book.class);

                }
                library.outputProcess.showCustomerMenu();


                //return userId;
            }


        },
        CHECKOUTMOVIE {
            @Override
            public void performOperation(Library library) {
                boolean isTheOperationSuccessful = false;
                String movieName = library.inputProcess.getEntityName("movie", "check out");
                try {
                    isTheOperationSuccessful = library.checkOut(movieName, library.getCurrentUser(), Movie.class);
                } catch (Exception e) {
                    System.out.println(e);
                }

                if (isTheOperationSuccessful)
                    library.outputProcess.printThankYouMessageForCheckingOut(Movie.class);

                library.outputProcess.showCustomerMenu();


               // return userId;
            }
        },
        RETURNBOOK {
            @Override
            public void performOperation(Library library) throws NotAValidUser {
                boolean isTheOperationSuccessful;
                String bookName = library.inputProcess.getEntityName("book", "return");
                try {
                    isTheOperationSuccessful = library.returnRentable(bookName, library.getCurrentUser(), Book.class);
                } catch (Exception e) {
                    System.out.println(e);
                    isTheOperationSuccessful = false;
                }

                if (isTheOperationSuccessful) {
                    library.outputProcess.printThankYouMessageForReturnin(Book.class);
                }
                library.outputProcess.showCustomerMenu();

               // return userId;
            }
        },
        RETURNMOVIE {
            @Override
            public void performOperation(Library library) throws NotAValidUser {
                String movieName = library.inputProcess.getEntityName("movie", "return");
                boolean isTheOperationSuccessful = false;
                try {
                    isTheOperationSuccessful = library.returnRentable(movieName, library.getCurrentUser(), Movie.class);
                } catch (Exception e) {
                    System.out.println(e);
                }
                if (isTheOperationSuccessful)
                    library.outputProcess.printThankYouMessageForReturnin(Movie.class);
                library.outputProcess.showCustomerMenu();

                //return userId;
            }

        },
        SEEMYDETAILS {
            @Override
            public void performOperation(Library library) throws NotAValidUser {
                User user = library.seeMyDetails(library.getCurrentUser());
                library.outputProcess.printUserDetails(user);
                //return userId;
            }
        },
        LOGOUT {
            @Override
            public void performOperation(Library library) throws NotAValidUser {
                library.logOut(library.getCurrentUser());
                library.outputProcess.logOut();
               // return userId;
            }
        },
        QUIT {
            @Override
            public void performOperation(Library library) throws NotAValidUser {
                library.outputProcess.printThankYouWhenTheUserQuits();
                System.exit(0);
                //return null;
            }
        },

        WHOHASTHISENTITY {
            @Override
            public void performOperation(Library library) throws NotAValidUser, NoSuchEntity {
                String entityName = library.inputProcess.getEntityName("entity", "check who has it");
                String libraryNumber = library.whoHasThisEntity(entityName);
                library.outputProcess.printWhoHAsIt(libraryNumber);


                //return userId;
            }
        },
        UNKNOWN {
            @Override
            public void performOperation(Library library) throws NotAValidUser {
                library.outputProcess.whenUnknownOptionIsSelected();
                library.outputProcess.showGeneralMenu();
                //return null;
            }
        }

    }


    public static void main(String[] args) throws NotAValidUser, NoSuchEntity {
        Library library = new Library();
        String userId=" ";
        library.outputProcess.showGeneralMenu();
        int option;
do {
    option = library.inputProcess.getUserOption();

   Operations.values()[option].performOperation(library);
}while (!(userId.equals("")));

}}